package com.gilgamesh.mall.product.service.impl;

import com.gilgamesh.mall.product.dao.ProductDetailDao;
import com.gilgamesh.mall.product.service.ProductDetailService;
import com.gilgamesh.mall.sdk.bean.product.ProductDetailEntity;
import com.gilgamesh.mall.sdk.common.RedisConstant;
import com.gilgamesh.mall.sdk.dto.ResponseDTO;
import com.gilgamesh.mall.sdk.dto.product.ProductDetailDTO;
import com.gilgamesh.mall.sdk.util.BeanUtil;
import com.gilgamesh.mall.sdk.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Random;

/**
 * Created by Peiyao.Wang on 2021/1/13
 *
 * @author Peiyao.Wang
 * @date 2021/1/13
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductDetailServiceImpl implements ProductDetailService {

    private final ProductDetailDao productDetailDao;

    private final RedisTemplate redisTemplate;

    @Override
    public ResponseDTO<ProductDetailDTO> getProductDetailById(Long id) {
        ResponseDTO<ProductDetailDTO> responseDTO = new ResponseDTO<>();
        ProductDetailDTO productDetailDTO = productDetailDao.getProductDetailById(id);
        responseDTO.setData(productDetailDTO);
        if(BeanUtil.isLogicalNull(responseDTO)){
            responseDTO.setCode("0");
            responseDTO.setMessage("success");
            responseDTO.setSuccess(Boolean.TRUE);
        }
        return responseDTO;
    }

    @Override
    public ResponseDTO<List<ProductDetailDTO>> listProductDetail(ProductDetailEntity productDetailEntity) {
        if (log.isDebugEnabled()) {
            log.debug("productEntity is,{}", productDetailEntity);
        }
        return ResponseUtil.getResponse(productDetailDao.listProductDetails(productDetailEntity));
    }

    @Override
    public ResponseDTO<Integer> add(ProductDetailEntity productDetailEntity) {
        productDetailEntity.setSkuCode(getSkuCode());
        return ResponseUtil.getResponse(productDetailDao.insert(productDetailEntity));
    }


    public String getSkuCode() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        boolean flag = false;
        while (!flag) {
            while (sb.length() < RedisConstant.SKU_CODE_LENGTH) {
                int get = random.nextInt(999999);
                while (get < 100000) {
                    get = random.nextInt(999999);
                }
                sb.append(get);
            }
            if (redisTemplate.opsForValue().get(RedisConstant.SKU_CODE + ":" + sb.toString()) == null) {
                flag = true;
                redisTemplate.opsForValue().set(RedisConstant.SKU_CODE + ":" + sb.toString(), sb.toString());
            } else {
                sb = new StringBuilder();
            }
        }
        return sb.toString();
    }
}
